package com.anjie.powerproject.repository;

import com.anjie.powerproject.entity.Point;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

public interface PointRepository extends JpaRepository<Point, Integer> {
    @Transactional
    @Modifying
    @Query(nativeQuery = true,value="update point set point_name = ?1,dev_code = ?2 where id = ?3")
    Integer updatePoint(String pointName,String devCode,Integer id);

    @Query(nativeQuery = true,value="select * from point where if(?1 !='',point_name like %?1%,1=1) and if(?2 !='',dev_code like %?2%,1=1)")
    List<Point> getPointListByConditions(String pointName,String devCode);

    @Query(nativeQuery = true,value="select count(1) from point where if(?1 !='',point_name=?1 ,1=1) or if(?2 !='',dev_code=?2,1=1)")
    Integer pointIsExist(String pointName,String devCode);

    @Query(nativeQuery = true,value="select count(1) from point where if(?1 !='',dev_code=?1,1=1)")
    Integer pointDevCodeIsExist(String devCode);

    @Query(nativeQuery = true,value="select count(1) from point where if(?1 !='',point_name=?1 ,1=1)")
    Integer pointNameIsExist(String pointName);

    @Query(nativeQuery = true,value="select dev_code from point")
    List<String> getAllPointDevCode();

    @Query(nativeQuery = true,value="select dev_code from point where if(?1 !='',dev_code like %?1%,1=1)")
    List<String> fuzzySelectPointDevCode(String devCode);

    @Query(nativeQuery = true,value="select * from point where dev_code = ?1")
    Point getPointByDevCode(String devCode);

}
